#include <iostream>
#include <string>
#include <cstdio>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
    // 1.以写方式打开管道文件
    int fd = open("fifo_for_copy", O_WRONLY);
    if (fd < 0)
    {
        perror("open");
        exit(2);
    }
    // 以读方式打开待拷贝的文件
    int need = open("log.txt", O_RDONLY);
    if (need < 0)
    {
        perror("open");
        exit(2);
    }

    // 读need，写入管道文件中
    char buffer[1024];
    ssize_t n = 0;
    while ((n = read(need, buffer, sizeof(buffer))) > 0)
    {
        write(fd, buffer, n);
    }

    close(fd);
    close(need);

    return 0;
}